﻿using QuanLyThuVien.DAL;
using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThuVien.BLL
{
    //----------------------------------------------------------------------
    //Desc: class xử lý cho bảng PHIEUTHU
    //----------------------------------------------------------------------
    public class BLLPhieuThu
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public BLLPhieuThu()
        { }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách phiếu thu
        //----------------------------------------------------------------------
        public DataTable LayDanhSachPhieuThu()
        {
            DALPhieuThu _dalPhieuThu = new DALPhieuThu();
            return _dalPhieuThu.LayDanhSachPhieuThu();
        }

        //----------------------------------------------------------------------
        //Desc: lấy phiếu thu
        //----------------------------------------------------------------------
        public DTOPhieuThu LayPhieuThu(string maphieuthu)
        {
            DALPhieuThu _dalPhieuThu = new DALPhieuThu();
            DTOPhieuThu phieuthu = _dalPhieuThu.LayPhieuThu(maphieuthu);
            if (phieuthu == null)
                return null;
            DALDocGia _dalDocGia = new DALDocGia();
            DTODocGia docgia = _dalDocGia.LayThongTinDG(phieuthu.DocGia.MaDG);
            if (docgia == null)
                return null;
            phieuthu.DocGia = docgia;
            return phieuthu;
        }

        //----------------------------------------------------------------------
        //Desc: Lấy danh sách phiếu thu của độc giả
        //----------------------------------------------------------------------
        public DataTable LayPhieuThuDocGia(string madocgia)
        {
            DALPhieuThu _dalPhieuThu = new DALPhieuThu();
            return _dalPhieuThu.LayPhieuThuDocgia(madocgia);
        }

        //----------------------------------------------------------------------
        //Desc: phát sinh mã phiếu thu
        //----------------------------------------------------------------------
        public bool PhatSinhMaPhieuThu(ref DTOPhieuThu phieuthu)
        {
            DALPhieuThu _dalPhieuThu = new DALPhieuThu();

            int stt = _dalPhieuThu.DemSoPhieuThu();
            if (stt < 0)
                return false;
            if (stt == 0)
                stt++;

            string maphieu = "PTH" + stt.ToString("D3");
            int dem = _dalPhieuThu.DemMaPhieuThu(maphieu);

            //có thể xảy ra vòng lặp vô hạn
            while (dem > 0)
            {
                stt++;
                maphieu = "PTH" + stt.ToString("D3");
                dem = _dalPhieuThu.DemMaPhieuThu(maphieu);
            }

            if (dem < 0)
                return false;

            phieuthu.MaPhieuThu = maphieu;
            return true;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật phiếu thu
        //----------------------------------------------------------------------
        public bool CapNhat(DTOPhieuThu phieuthu)
        {
            DALPhieuThu _dalPhieuThu = new DALPhieuThu();
            if (_dalPhieuThu.CapNhat(phieuthu))
                return true;
            else
                return false;
        }

    }
}
